/*NOTICE

This software was produced for the U. S. Government
under Contract No. FA8721-09-C-0002, and is
subject to the Rights in Noncommercial Computer Software
and Noncommercial Computer Software Documentation Clause
(DFARS) 252.227-7014 (JUN 1995)

(c) 2009 The MITRE Corporation. All Rights Reserved.
*/
package org.mitre.xmltransform.spmif;

import org.mitre.xmltransform.XmlInputDocument;
import org.mitre.xmltransform.XmlOutputDocument;
//import org.mitre.xmltransform.uml2tospmif.Uml2InputDocument;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ExternalReferenceStep extends HierarchyStep {
	public ExternalReferenceStep(String pId, String pExecId, Node pNode, XmlInputDocument pInput, XmlOutputDocument pOutput) {
		super(pId, pExecId, pNode, pInput.buildNodeName(pNode, "REF"), pInput, pOutput);
		mPSName = null;
		mPSFile = null;
	}
	
	public String getIdPrefix() {
		return "EXTREF";
	}

	public void setPSName(String pPSName, String pPSFile) {
		mPSName = pPSName;
		mPSFile = pPSFile;
	}
	
	public void addChild(HierarchyStep pChild) {
		throw new IllegalArgumentException("External Reference steps can not have children");
	}

	public void generateStep(Element pParentElement, Device pLocation) {
		Node node = getNode();
		Element expandedNode = mOutput.addElement(pParentElement, "LinkNode", null);
		expandedNode.setAttribute("NodeName", getNodeName());
		if (mInput.evaluateBooleanInputExpression(node, "boolean(self::UML2:InteractionOccurrence)")) {
			mInput.setVariable("interactionId", mInput.getInputValue(node, "UML2:InteractionOccurrence.refersTo/UML2:Interaction/@xmi.idref"));
			expandedNode.setAttribute("PerformanceScenarioName", mInput.getInputValue(node, "ancestor::UML:Model/descendant::UML2:Interaction[@xmi.id=$interactionId]/@name"));
			expandedNode.setAttribute("PerformanceScenarioFilename", mOutput.getOutputFileName());
			
		} else {
			if (mPSFile == null) throw new IllegalArgumentException("must provide the PS file name");
			expandedNode.setAttribute("PerformanceScenarioFilename", mPSFile);
			if (mPSName != null) {
				expandedNode.setAttribute("PerformanceScenarioName", mPSName);
			}
		}
	}

	public String generateArcs(Element pParentElement, String pFromNode) {
		if (getNodeName() == null) return pFromNode;
		if (pFromNode != null) {
			Element arcNode = mOutput.addElement(pParentElement, "Arc", null);
			arcNode.setAttribute("FromNode", pFromNode);
			arcNode.setAttribute("ToNode", getId());
		}
		return getId();
	}
	
	private String mPSName, mPSFile;
}